//
//  AreadyLoadTableViewController.m
//  Slice
//
//  Created by lanou on 16/6/23.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "AreadyLoadTableViewController.h"

@interface AreadyLoadTableViewController ()

@property (nonatomic, retain) NSMutableArray *dataArr;

@end

@implementation AreadyLoadTableViewController

-(NSMutableArray *)dataArr {
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)getData {
    NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:locaFolderPath error:nil];
    self.dataArr = (NSMutableArray *)arr;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    [self getData];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"321"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"321" forIndexPath:indexPath];
    
    cell.textLabel.text = self.dataArr[indexPath.row];
    
    return cell;
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView beginUpdates];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
        NSString *path = [locaFolderPath stringByAppendingPathComponent:self.dataArr[indexPath.row]];
        
        [self.dataArr removeObject:self.dataArr[indexPath.row]];
        
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self getData];
            [self.tableView reloadData];
        });
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
       
    }
    [tableView endUpdates];
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
